﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace MFTool
{
    public static class StringExtension
    {
        public static string ReplaceArg(this string source, params object[] arg)
        {
            if (string.IsNullOrEmpty(source)) return source;
            return string.Format(source, arg);
        }

        public static string ReplaceBraceContent(this string source, Dictionary<string, object> dic)
        {
            if (string.IsNullOrEmpty(source)) return source;

            foreach (var p in dic)
            {
                source = source.Replace("{" + p.Key + "}", (p.Value ?? "").ToString());
            }
            return source;
        }

        public static IEnumerable<string> GetInnerContents(this string source, string startStr = "{", string endStr = "}")
        {
            List<string> resList = new List<string>();
            if (!string.IsNullOrEmpty(source))
            {
                Regex rg = new Regex("(?<=(" + startStr + "))[.\\s\\S]*?(?=(" + endStr + "))",
                    RegexOptions.Multiline | RegexOptions.Singleline);

                MatchCollection matches = rg.Matches(source);

                foreach (Match item in matches)
                    resList.Add(item.Value);
            }
            return resList;
        }
    }
}
